#include "stdint.h"
#include "stddef.h"
#include "cpu_peri.h"
#include <bk_timer.h>
#include <cpu_peri_gpio.h>
#include <bk_timer_pub.h>

#include "app_led.h"

#ifdef APP_LED_SUPPORT

#define LED_RED_ON_TIME        50
#define LED_RED_OFF_TIME       50

typedef struct led_info
{
        /* when LED state is change then the led is start on/off */
        BOOLEAN bStartOnOFF;
        /* in this state, the led control sum */
        uint8 cLedCtrSum;
        uint16 wOnTime;
        uint16 wOffTime;
        /* repeat delay time */
        uint16 wRptDlyTime;
}LED_INFO_T;

bool_t bRedLedCtr = FALSE;
LED_INFO_T emRedLed = {0};
LED_INFO_T emCustomLed = {0};
uint8 cRedLedCtrCount = 0;

/* led state */
LED_STATE s_eLedState    = LED_MAX_STATE;
LED_STATE s_eLedActiveState = LED_MAX_STATE;


ptu32_t led_event_init(void);
bool_t led_EventMain(void);
void LedChangeState(LED_STATE eState);

#if 0
/**--------------------------------------------------------------------------*/
void ledStartCtrTimer(uint32 time);
/* led ctr timer func */
void LedCtrTimerFuc(UINT8 time);

extern void bk_timer_init(void);
extern UINT32 bk_timer_ctrl(UINT32 cmd, void *param);


void LedTimerInit(void)
{
    /* init led ctr timer */
//    timer_param_t test_param;
    bk_timer_init();
    bk_timer_ctrl(CMD_TIMER_UNIT_ENABLE, 0);
}
#endif

void LedSetState(LED_STATE eState)
{

    s_eLedState = eState;
    LedChangeState(s_eLedState);

//    ledStartCtrTimer(100);
}

void LedChangeState(LED_STATE eState)
{
    bRedLedCtr = TRUE;
    cRedLedCtrCount = 0;

    emRedLed.wOnTime  = LED_RED_ON_TIME;
    emRedLed.wOffTime = LED_RED_OFF_TIME;

    switch(eState)
    {
        case LED_WIFI_DIRECT_CFG_WAIT_STATE:{  /* wifi wait connet */
                emRedLed.cLedCtrSum = 4;
                emRedLed.bStartOnOFF = LED_ON;
                emRedLed.wRptDlyTime = 1000;
                break;}

        case LED_WIFI_CONNECT_WAIT_STATE:
                emRedLed.cLedCtrSum = 2;
                emRedLed.bStartOnOFF = LED_OFF;
                emRedLed.wOnTime  = 200;
                emRedLed.wOffTime = 150;
                emRedLed.wRptDlyTime = 50;
                break;

        case LED_WIFI_INTNET_WAIT_STATE:{/* wifi connet wait internet */
                emRedLed.cLedCtrSum = 2;
                emRedLed.bStartOnOFF = LED_ON;
                emRedLed.wOnTime  = 500;
                emRedLed.wOffTime = 500;
                emRedLed.wRptDlyTime = 1000;
                break;}
        case LED_WIFI_INTNET_SUCCESS_STATE:{  /*  internet is ok */
                emRedLed.cLedCtrSum = 1;
                emRedLed.bStartOnOFF = LED_OFF;
                emRedLed.wRptDlyTime = 0;
                break;}
        default:{break;}
    }

}

#if 0
void ledStartCtrTimer(uint32 time)
{
    timer_param_t test_param;
    test_param.channel = 0;
    test_param.div = 1;
    test_param.period = time;
    test_param.t_Int_Handler = LedCtrTimerFuc;
    bk_timer_ctrl(CMD_TIMER_INIT_PARAM,&test_param);
}

void LedCtrTimerFuc(UINT8 time)
{
    uint16 wLedOnOffTime = 0;
      if(!time)
      {
           if(TRUE == bRedLedCtr)
           {
                   if(cRedLedCtrCount & 0x01)
                   {
                           wLedOnOffTime = emRedLed.wOffTime;
                           ledRed(!(emRedLed.bStartOnOFF));
                   }
                   else
                   {
                           wLedOnOffTime = emRedLed.wOnTime;
                           ledRed(emRedLed.bStartOnOFF);
                   }
                   cRedLedCtrCount++;
           }

           switch(s_eLedActiveState)
           {
                   case LED_WIFI_INTNET_SUCCESS_STATE:/*  internet is ok */
                           bRedLedCtr = FALSE;
                           wLedOnOffTime = 0;
                           break;
                   case LED_WIFI_DIRECT_CFG_WAIT_STATE:/* wifi wait config */
                   case LED_WIFI_CONNECT_WAIT_STATE:   /* wifi wait connet */
                   case LED_WIFI_INTNET_WAIT_STATE:    /* internet wait */
                   {
                           if(s_eLedActiveState != s_eLedState)
                           {
                                   s_eLedActiveState = s_eLedState;
                                   LedChangeState(s_eLedActiveState);
//                                   wLedOnOffTime = 1000;
                           }
                           break;
                   }
                   default:
                   {
                       if(s_eLedActiveState != s_eLedState)
                        {
                            s_eLedActiveState = s_eLedState;
                            LedChangeState(s_eLedActiveState);
//                            wLedOnOffTime = 1000;
                        }
                   }
                           break;
           }

           if((TRUE == bRedLedCtr) && (cRedLedCtrCount == emRedLed.cLedCtrSum))
           {
                   cRedLedCtrCount = 0;
                   wLedOnOffTime = emRedLed.wRptDlyTime;
           }

           if(wLedOnOffTime)
           {
               ledStartCtrTimer(wLedOnOffTime);
           }
      }
}
#endif

bool_t led_event_init(void)
{
    u16 evtt_led;
    evtt_led = Djy_EvttRegist(EN_CORRELATIVE, CN_PRIO_RRS, 0, 0, led_EventMain, NULL, 0x1000, "led_evevt");
    if(evtt_led != CN_EVENT_ID_INVALID)
    {
        Djy_EventPop(evtt_led, NULL, 0, NULL, 0, 0);
    }else
    {
        printf("\r\nRingBuf Event Start Fail!\r\n");
    }

    return true;
}

ptu32_t led_EventMain(void)
{
    uint16 wLedOnOffTime = 0;
    while(1)
    {
        if(TRUE == bRedLedCtr)
        {
                if(cRedLedCtrCount & 0x01)
                {
                        wLedOnOffTime = emRedLed.wOffTime;
                        ledRed(!(emRedLed.bStartOnOFF));
                }
                else
                {
                        wLedOnOffTime = emRedLed.wOnTime;
                        ledRed(emRedLed.bStartOnOFF);
                }
                cRedLedCtrCount++;
        }
        if((TRUE == bRedLedCtr) && (cRedLedCtrCount == emRedLed.cLedCtrSum))
        {
                cRedLedCtrCount = 0;
                wLedOnOffTime = emRedLed.wRptDlyTime;
        }
        Djy_EventDelay(wLedOnOffTime*1000);
    }
    return 0;
}

void ledRed(bool_t on)
{
    djy_gpio_write(GPIO12,on);
}

#endif
